<template>
  <div id="login">
    <div class="login-wrap">
      <ul class="menu-tab">
        <li
          v-for="item in menuTab"
          :class="{ current: item.current }"
          :key="item.id"
          @click="toggelMenu(item)"
        >
          {{ item.txt }}
        </li>
      </ul>

      <!-- 表单 start -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="login-form"
        size="medium"
      >
        <el-form-item prop="username" class="item-form">
          <label>邮箱</label>
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password" class="item-form">
          <label>密码</label>
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            minlength="6"
            maxlength="20"
          ></el-input>
        </el-form-item>

        <el-form-item prop="passwords" class="item-form" v-if ="model === 'register'">
          <label>再次输入密码</label>
          <el-input
            type="password"
            v-model="ruleForm.passwords"
            autocomplete="off"
            minlength="6"
            maxlength="20"
          ></el-input>
        </el-form-item>

        <el-form-item prop="code" class="item-form">
          <label>验证码</label>
          <el-row :gutter="20">
            <el-col :span="15"
              ><el-input v-model.number="ruleForm.code" maxlength="6"></el-input
            ></el-col>
            <el-col :span="9">
              <el-button type="primary" class="block">获取验证码</el-button>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item class="item-form">
          <el-button
            type="danger"
            @click="submitForm('ruleForm')"
            class="login-btn block"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  stripscript,
  validatePass,
  validateEmail,
  validateVCode
} from "@/utils/validate";
import { reactive } from 'node_modules/@vue/composition-api';

export default {
  name: "login",
  setup(props,context) {
    // 这里面放置data数据，生命周期，自定义函数
    // 声明对象类型的数据要用reactive
    const menuTab = reactive([
      { txt: "登录", current: true ,type:'login'},
      { txt: "注册", current: false,type:'register'}
    ])
    console.log(menuTab);

  },
  data() {
    // 验证邮箱
    var validateUsername = (rule, value, callback) => {
      // let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (validateEmail(value)) {
        callback(new Error("用户格式有误"))
      } else {
        callback(); //返回true
      }
    };

    // 验证密码
    var validatePassword = (rule, value, callback) => {
      console.log(stripscript(value));
      // 过滤后的密码数据
      this.ruleForm.password = stripscript(value)
      value = this.ruleForm.password
      // let reg = /^(?!\D+$)(?![^a-zA-Z]+$)\S{6,20}$/
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (validatePass(value)) {
        callback(new Error("密码为数字+字母"));
      } else {
        callback();
      }
    };

    var validatePasswords = (rule, value, callback) => {
          //如果模块值为login，直接通过，不会再运行后面程序
          // if(this.model = 'login'){
          //   return callback()
          // }
          console.log(stripscript(value));
          // 过滤后的密码数据
          if (value === "") {
            callback(new Error("请输入密码"));
          } else if (this.ruleForm.password!=value) {
            callback(new Error("与第一次密码不符"));
          } else {
            callback();
          }
        };



     // 验证验证码
    let validateCode = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请输入验证码"));
      } else if (validateVCode(value)) {
        return callback(new Error("验证码格式有误"));
      } else {
        callback();
      }
    };
    return {
      menuTab: [
        { txt: "登录", current: true ,type:'login'},
        { txt: "注册", current: false,type:'register' },
      ],
      // 模块值 用于切换注册和登录
      model:"login",
      
      
      // 表单的数据
      ruleForm: {
        username: "",
        password: "",
        code: "",
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
        passwords:[{ validator: validatePasswords, trigger: "blur" }],
        code: [{ validator: validateCode, trigger: "blur" }],
      },
    };
  },
  methods: {
    toggelMenu(data) {
      this.menuTab.forEach((elem) => {
        elem.current = false;
      });
      // 每次点击，都将所有改为false，再传入点击的对象，改为true
      data.current = true;
      this.model = data.type
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
#login {
  height: 100vh;
  background-color: #344a5f;
}
.login-wrap {
  width: 330px;
  margin: 0 auto;
}
.menu-tab {
  text-align: center;
  li {
    display: inline-block;
    width: 88px;
    line-height: 36px;
    font-size: 14px;
    color: #fff;
  }
  .current {
    background-color: rgba(0, 0, 0, 0.1);
  }
}

.login-form {
  margin-top: 9px;
  label {
    margin-bottom: 3px;
    display: block;
    font-size: 14px;
    color: #fff;
  }
  .item-form {
    margin-bottom: 13px;
  }
  .block {
    display: block;
    width: 100%;
    height: 33px;
  }
  .login-btn {
    margin-top: 19px;
  }
}
</style>
